package com.nineclock.document.controller;

import com.nineclock.api.document.AppDocumentControllerApi;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.document.dto.*;
import com.nineclock.document.service.DocCollaborationsService;
import com.nineclock.document.service.DocFileService;
import com.nineclock.document.service.DocFolderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhangyan
 * @date 2021/4/7 15:19
 */
@RestController

public class DocumentController implements AppDocumentControllerApi {

    @Autowired
    private DocFileService fileService;
    @Autowired
    private DocFolderService folderService;
    @Autowired
    private DocCollaborationsService collaborationsService;

    /**
     * 查询当前目录下的 子文件夹及文件
     *
     * @param parentFolderId
     * @return
     */
    @Override
    @GetMapping("/listFolderAndFile")
    public Result<DocumentDto> listFolderAndFile(@RequestParam(value = "parentFoldId", defaultValue = "0") Long parentFolderId) {  //如果不传,默认值为0,查询顶级文件夹

        DocumentDto documentDto = fileService.listFolderAndFile(parentFolderId);
        return Result.success(documentDto);
    }

    /**
     * 新建文件夹
     *
     * @param docFolderDto
     * @return
     */
    @Override
    @PostMapping("/insertFolder")
    public Result insertFolder(@RequestBody DocFolderDto docFolderDto) {
        folderService.insertFolder(docFolderDto);
        return Result.success(ResponseEnum.SUCCESS);
    }

    /**
     * 根据文档ID查询文档信息
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/getFileByFileId")
    public Result<DocFileDto> getFileByFileId(@RequestParam("id") Long id) {
        DocFileDto fileDto = fileService.getFileByFileId(id);
        return Result.success(fileDto);
    }

    /**
     * 根据文档ID分页查询文档协作者
     *
     * @param fileId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    @GetMapping("/pagingCollaborations")
    public Result<PageResult<DocumentUserDTO>> pagingCollaborations(
            @RequestParam(value = "id") Long fileId,
            @RequestParam(value = "page", required = false, defaultValue = "1") Long page,
            @RequestParam(value = "pageSize", required = false, defaultValue = "7") Long pageSize
    ) {

        PageResult<DocumentUserDTO> pageResult = fileService.pagingCollaborations(fileId, page, pageSize);

        return Result.success(pageResult);
    }

    /**
     * 根据文件ID获取用户信息 , 标识那些是拥有者 , 那些是协作者
     *
     * @param fileId
     * @return
     */
    @Override
    @GetMapping("/pagingUsers")
    public Result<List<DocUserCollaborationDTO>> pageUsers(@RequestParam("id") Long fileId) {

        List<DocUserCollaborationDTO> list = collaborationsService.pagingUsers(fileId);
        return Result.success(list);
    }
}
